package com.RMP
{
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;
    import com.greensock.*;

    public class Slideshow extends Sprite
    {
        public var init:Boolean;
        var tilesOrderArray:Array;
        var xmlLoader:URLLoader;
        var timerSpeed:Number;
        var fadeSpeed:Number;
        var curr:Number;
        var image:Bitmap;
        var photoArray:Array;
        var photos:XMLList;
        var tiles:Sprite;
        var myRoot:MovieClip;
        var tile:Sprite;
        var tilesArray:Array;
        var photoLoader:Loader;
        var theXML:XMLList;

        public function Slideshow(param1:MovieClip, param2:XMLList, param3:Number) : void
        {
            myRoot = param1;
            theXML = param2;
            fadeSpeed = param3;
            photoArray = new Array();
            curr = 0;
            timerSpeed = 1000;
            tilesOrderArray = new Array(0, 1, 2, 5, 4, 3);
            init = false;
            buildTiles();
            getXML();
            myRoot.stage.addEventListener(Event.RESIZE, onResizer, false, 0, true);
            doResize();
            return;
        }

        function doResize() : void
        {
            var _loc_1:int = 0;
            if (image != null)
            {
                image.width = myRoot.gridCurrW;
                image.height = myRoot.gridCurrH;
                if (image.scaleX < 1)
                {
                    image.smoothing = true;
                }
                else
                {
                    image.smoothing = false;
                }
            }
            _loc_1 = 0;
            while (_loc_1 < tilesArray.length)
            {
                
                tilesArray[_loc_1].width = Math.round(myRoot.gridCurrW / 3);
                tilesArray[_loc_1].height = Math.round(myRoot.gridCurrH / 2);
                tilesArray[_loc_1].x = Math.round(_loc_1 % 3 * (myRoot.gridCurrW / 3));
                tilesArray[_loc_1].y = Math.round(Math.floor(_loc_1 / 3) * (myRoot.gridCurrH / 2));
                _loc_1++;
            }
            return;
        }

        function randomizeArray(param1:Array) : Array
        {
            var _loc_2:Array = null;
            _loc_2 = new Array();
            while (param1.length > 0)
            {
                
                _loc_2.push(param1.splice(Math.floor(Math.random() * param1.length), 1));
            }
            return _loc_2;
        }

        private function loadComplete(event:Event) : void
        {
            if (image != null)
            {
                fadeOut();
            }
            else
            {
                fadeIn();
            }
            return;
        }

        function fadeOut() : void
        {
            image.smoothing = false;
            TweenMax.to(image, fadeSpeed * 2, {alpha:0, overwrite:true, onComplete:function ()
            {
                removeChild(image);
                image = null;
                fadeIn();
                return;
            }
            });
            return;
        }

        function fadeInTiles() : void
        {
            var _loc_1:int = 0;
            doResize();
            _loc_1 = 0;
            while (_loc_1 < 6)
            {
                
                tilesArray[tilesOrderArray[_loc_1]].alpha = 0;
                tilesArray[tilesOrderArray[_loc_1]].visible = false;
                _loc_1++;
            }
            return;
        }

        function onResizer(event:Event)
        {
            doResize();
            return;
        }

        private function switchPhoto() : void
        {
            if (curr < photoArray.length)
            {
                photoLoader = new Loader();
                photoLoader.load(new URLRequest(myRoot.path + photoArray[curr]));
                var _loc_2:* = curr + 1;
                curr = _loc_2;
                photoLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete, false, 0, true);
            }
            else
            {
                curr = 0;
                switchPhoto();
            }
            return;
        }

        function fadeOutTiles() : void
        {
            var counter:*;
            var i:int;
            counter;
            doResize();
            i;
            while (i < 6)
            {
                
                tilesArray[tilesOrderArray[i]].alpha = 0;
                tilesArray[tilesOrderArray[i]].visible = true;
                TweenMax.to(tilesArray[tilesOrderArray[i]], fadeSpeed, {alpha:1, overwrite:true, delay:i * 0.05, onComplete:function ()
            {
                var _loc_2:* = counter + 1;
                counter = _loc_2;
                if (counter == 6)
                {
                    removeChild(image);
                    image = null;
                    fadeIn();
                }
                return;
            }
            });
                i = (i + 1);
            }
            return;
        }

        public function stopTween()
        {
            TweenMax.killTweensOf(image);
            return;
        }

        function restartTimer() : void
        {
            setTimeout(switchPhoto, timerSpeed);
            return;
        }

        function buildTiles() : void
        {
            var _loc_1:int = 0;
            tiles = new Sprite();
            tilesArray = new Array();
            _loc_1 = 0;
            while (_loc_1 < 6)
            {
                
                tilesArray[_loc_1] = new Sprite();
                tilesArray[_loc_1].graphics.beginFill(16777215, 100);
                tilesArray[_loc_1].graphics.drawRect(0, 0, 1, 1);
                tilesArray[_loc_1].graphics.endFill();
                tilesArray[_loc_1].alpha = 1;
                tilesArray[_loc_1].visible = false;
                tiles.addChild(tilesArray[_loc_1]);
                _loc_1++;
            }
            addChild(tiles);
            doResize();
            return;
        }

        function fadeIn() : void
        {
            image = Bitmap(photoLoader.content);
            image.alpha = 0;
            addChild(image);
            addChild(tiles);
            photoLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadComplete);
            photoLoader = null;
            if (init == false)
            {
                init = true;
                dispatchEvent(new SlideshowInitEvent());
            }
            doResize();
            TweenMax.to(image, fadeSpeed * 4, {alpha:1, overwrite:true, onComplete:function ()
            {
                restartTimer();
                return;
            }
            });
            return;
        }

        private function getXML() : void
        {
            var _loc_1:int = 0;
            photos = new XMLList(theXML.slide);
            _loc_1 = 0;
            while (_loc_1 < photos.length())
            {
                
                photoArray.push(photos[_loc_1].@url);
                _loc_1++;
            }
            photoArray = randomizeArray(photoArray);
            switchPhoto();
            return;
        }

    }
}
